﻿namespace OsPortal
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Caching;
    using System.Web.Configuration;
    using System.Web.Security;

    public class oStringHelper
    {
        public const string CRLF = "\r\n";
        public static string strDe = "-+!@<}xGHId_MNOP']4XYZov#$%ABC)~.DJKLuq2>/EF^&*zTUV(,?tpigecayl1973jhfQn6805mk[{|+=b";
        public static string strEn = "^&*(]CDGH,?|+=1290a6bc-}[{+!@#$EF%dejkno)~fghi.B<78lm>/'pIJKLMNqtuv345xyz_AOPQTUVXYZ";
        private static readonly string[] VietnameseSigns = new string[] { "aAeEoOuUiIdDyY", "\x00e1\x00e0ạả\x00e3\x00e2ấầậẩẫăắằặẳẵ", "\x00c1\x00c0ẠẢ\x00c3\x00c2ẤẦẬẨẪĂẮẰẶẲẴ", "\x00e9\x00e8ẹẻẽ\x00eaếềệểễ", "\x00c9\x00c8ẸẺẼ\x00caẾỀỆỂỄ", "\x00f3\x00f2ọỏ\x00f5\x00f4ốồộổỗơớờợởỡ", "\x00d3\x00d2ỌỎ\x00d5\x00d4ỐỒỘỔỖƠỚỜỢỞỠ", "\x00fa\x00f9ụủũưứừựửữ", "\x00da\x00d9ỤỦŨƯỨỪỰỬỮ", "\x00ed\x00ecịỉĩ", "\x00cd\x00ccỊỈĨ", "đ", "Đ", "\x00fdỳỵỷỹ", "\x00ddỲỴỶỸ" };

        public static string CatChuoi(string s, int length)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "";
            }
            string[] strArray = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (strArray[0].Length > length)
            {
                return s;
            }
            StringBuilder builder = new StringBuilder();
            foreach (string str in strArray)
            {
                if ((builder + str).Length > length)
                {
                    return string.Format("{0}...", builder.ToString().TrimEnd(new char[] { ' ' }));
                }
                builder.Append(str + " ");
            }
            return string.Format("{0}...", builder.ToString().TrimEnd(new char[] { ' ' }));
        }

        public static string CatTieuDeKoDau(string strValue, int intNumber)
        {
            string str = "";
            if (strValue.Length <= intNumber)
            {
                return strValue;
            }
            str = strValue.Substring(0, intNumber);
            if (str[str.Length - 1] == '-')
            {
                str = str.Substring(0, str.Length - 1);
            }
            return str;
        }

        public static string CleanWordHtml(string html)
        {
            StringCollection strings = new StringCollection();
            strings.Add(@"<(/?span|!\[)[^>]*?>");
            strings.Add(@"<(/?div|!\[)[^>]*?>");
            strings.Add(@"<!--(\w|\W)+?-->");
            strings.Add(@"<title>(\w|\W)+?</title>");
            strings.Add(@"<(meta|link|/?o:|/?style|/?font|/?st\d|/?head|/?html|body|/?body|!\[)[^>]*?>");
            strings.Add("(<[^/][^(th|d)>]*>){1}(&nbsp;)*(</[^>]+>){1}");
            strings.Add("\\s+v:\\w+=\"[^\"]+\"");
            strings.Add("(" + Environment.NewLine + "){2,}");
            strings.Add("( ){2,}");
            foreach (string str in strings)
            {
                html = Regex.Replace(html, str, "", RegexOptions.IgnoreCase);
            }
            return html;
        }

        public static bool ClearCache()
        {
            try
            {
                Cache cache = HttpRuntime.Cache;
                List<string> list = new List<string>();
                foreach (DictionaryEntry entry in cache)
                {
                    list.Add((string) entry.Key);
                }
                foreach (string str in list)
                {
                    cache.Remove(str);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static string ClearTagHTML(string str)
        {
            return Regex.Replace(str, @"<(.|\n)*?>", string.Empty);
        }

        public static string ConvertDateTime_to_String(DateTime dt, int type)
        {
            string str = "";
            try
            {
                string str2 = dt.Hour.ToString();
                string str3 = dt.Minute.ToString();
                string str4 = dt.Year.ToString();
                string str5 = dt.Month.ToString();
                string str6 = dt.Day.ToString();
                if (type == 1)
                {
                    str = str2 + ":" + str3 + " - " + str6 + "/" + str5 + "/" + str4;
                }
            }
            catch
            {
            }
            return str;
        }

        public static string ConvertDecoding(string ConvertStr)
        {
            if (ConvertStr.Length == 0)
            {
                return ConvertStr;
            }
            return ConvertEncodingDecoding(strEn, strDe, ConvertStr);
        }

        public static string ConvertEncoding(string ConvertStr)
        {
            if (ConvertStr.Length == 0)
            {
                return ConvertStr;
            }
            return ConvertEncodingDecoding(strDe, strEn, ConvertStr);
        }

        private static string ConvertEncodingDecoding(string _strCon1, string _strCon2, string _ConvertStr)
        {
            string str = string.Empty;
            for (int i = 0; i < _ConvertStr.Length; i++)
            {
                int index = _strCon1.IndexOf(_ConvertStr[i]);
                if (index >= 0)
                {
                    str = str + _strCon2[index];
                }
                else
                {
                    str = str + _ConvertStr[i];
                }
            }
            return str;
        }

        public static string ConvertNumberToString(double Value_)
        {
            string str;
            try
            {
                string[] strArray = new string[] { 
                    " kh\x00f4ng", " một", " hai", " ba", " bốn", " năm", " s\x00e1u", " bảy", " t\x00e1m", " ch\x00edn", " mươi", " mười", " mốt", " linh", " trăm", " ngh\x00ecn", 
                    " triệu", " tỷ"
                 };
                if (Value_ == 0.0)
                {
                    return strArray[0];
                }
                double num = Value_;
                int num2 = 0;
                for (int i = ((int) num) % 10; (i == 0) && !(num == 0.0); i = ((int) num) % 10)
                {
                    num2++;
                    num = ((int) num) / 10;
                }
                int index = 0;
                string[] strArray2 = new string[Value_.ToString().Length + 1];
                while (!(Value_ == 0.0))
                {
                    int num5 = ((int) Value_) % 10;
                    strArray2[index] = strArray[num5];
                    index++;
                    Value_ = ((int) Value_) / 10;
                }
                string str2 = string.Empty;
                int num6 = index;
                while (((index - num2) != 0) && (index != 0))
                {
                    switch ((index % 3))
                    {
                        case 0:
                            if (((strArray2[index - 1] != strArray[0]) || (strArray2[index - 2] != strArray[0])) || (strArray2[index - 3] != strArray[0]))
                            {
                                str2 = str2 + strArray2[index - 1] + strArray[14];
                            }
                            goto Label_0404;

                        case 1:
                            if (!(strArray2[index - 1] != strArray[0]))
                            {
                                goto Label_0404;
                            }
                            if ((((strArray2[index] != strArray[1]) && (strArray2[index - 1] == strArray[4])) && (num6 != 1)) && (num6 != index))
                            {
                                break;
                            }
                            goto Label_02C5;

                        case 2:
                            if (((!(strArray2[index - 1] != strArray[1]) && (num6 == index)) || (!(strArray2[index - 1] != strArray[1]) && !(strArray2[index] != strArray[0]))) || (strArray2[index - 1] == strArray[12]))
                            {
                                goto Label_02AE;
                            }
                            goto Label_0357;

                        default:
                            goto Label_0404;
                    }
                    str2 = str2 + strArray[4];
                    goto Label_0404;
                Label_02AE:
                    str2 = str2 + strArray[11];
                    goto Label_0404;
                Label_02C5:
                    if ((((strArray2[index] != strArray[0]) && (strArray2[index - 1] == strArray[5])) && (num6 != 1)) && (num6 != index))
                    {
                        str2 = str2 + strArray2[index - 1];
                    }
                    else if ((index == 1) && (strArray2[index - 1] == strArray[1]))
                    {
                        str2 = str2 + strArray[12];
                    }
                    else
                    {
                        str2 = str2 + strArray2[index - 1];
                    }
                    goto Label_0404;
                Label_0357:
                    if ((strArray2[index - 1] == strArray[0]) && (strArray2[index - 2] != strArray[0]))
                    {
                        str2 = str2 + " linh";
                    }
                    else if ((strArray2[index - 1] != strArray[0]) || (strArray2[index - 2] != strArray[0]))
                    {
                        if (strArray2[index - 1] != strArray[1])
                        {
                            str2 = str2 + strArray2[index - 1] + strArray[10];
                        }
                        else
                        {
                            str2 = str2 + strArray[11];
                        }
                    }
                Label_0404:
                    switch (index)
                    {
                        case 4:
                            if (((strArray2[index - 1] != strArray[0]) || (strArray2[index] != strArray[0])) || (strArray2[index + 1] != strArray[0]))
                            {
                                str2 = str2 + strArray[15];
                            }
                            break;

                        case 7:
                            if (((strArray2[index - 1] != strArray[0]) || (strArray2[index] != strArray[0])) || (strArray2[index + 1] != strArray[0]))
                            {
                                str2 = str2 + strArray[0x10];
                            }
                            break;

                        case 10:
                            if (((strArray2[index - 1] != strArray[0]) || (strArray2[index] != strArray[0])) || (strArray2[index + 1] != strArray[0]))
                            {
                                str2 = str2 + strArray[0x11];
                            }
                            break;
                    }
                    index--;
                }
                switch (num2)
                {
                    case 4:
                    case 5:
                        str2 = str2 + strArray[15];
                        break;

                    case 7:
                    case 8:
                        str2 = str2 + strArray[0x10];
                        break;

                    case 10:
                    case 11:
                        str2 = str2 + strArray[0x11];
                        break;
                }
                str = str2.Substring(1);
            }
            catch (Exception exception)
            {
                throw new Exception("Error StringHelper! - " + exception.Message);
            }
            return str;
        }

        public static string ConvertObjectToNumberString(object dec)
        {
            NumberFormatInfo numberFormat = new CultureInfo("en-US", false).NumberFormat;
            numberFormat.NumberDecimalDigits = 0;
            numberFormat.NumberDecimalSeparator = ",";
            numberFormat.NumberGroupSeparator = ".";
            if (dec is decimal)
            {
                decimal num = (decimal) dec;
                return num.ToString("N", numberFormat);
            }
            if (dec is int)
            {
                int num2 = (int) dec;
                return num2.ToString("N", numberFormat);
            }
            if (dec is float)
            {
                float num3 = (float) dec;
                return num3.ToString("N", numberFormat);
            }
            return dec.ToString();
        }

        public static string ConvertTcvn3ToUnicode(string ConvertStr)
        {
            return ConvertTcvn3ToUnicode(ConvertStr);
        }

        public static string ConvertUnicodeToTcvn3(string ConvertStr)
        {
            return ConvertUnicodeToTcvn3(ConvertStr);
        }

        public static string DateTimeVi(DateTime dtDatetime, int intType)
        {
            string str = "";
            try
            {
                string str2 = "";
                string str3 = "";
                if (dtDatetime.Day < 10)
                {
                    str3 = "0" + dtDatetime.Day.ToString();
                }
                else
                {
                    str3 = dtDatetime.Day.ToString();
                }
                string str4 = "";
                if (dtDatetime.Month < 10)
                {
                    str4 = "0" + dtDatetime.Month.ToString();
                }
                else
                {
                    str4 = dtDatetime.Month.ToString();
                }
                string str5 = dtDatetime.Year.ToString();
                int hour = dtDatetime.Hour;
                int minute = dtDatetime.Minute;
                int second = dtDatetime.Second;
                switch (dtDatetime.DayOfWeek)
                {
                    case DayOfWeek.Sunday:
                        str2 = "Chủ nhật";
                        break;

                    case DayOfWeek.Monday:
                        str2 = "Thứ hai";
                        break;

                    case DayOfWeek.Tuesday:
                        str2 = "Thứ ba";
                        break;

                    case DayOfWeek.Wednesday:
                        str2 = "Thứ tư";
                        break;

                    case DayOfWeek.Thursday:
                        str2 = "Thứ năm";
                        break;

                    case DayOfWeek.Friday:
                        str2 = "Thứ s\x00e1u";
                        break;

                    case DayOfWeek.Saturday:
                        str2 = "Thứ bảy";
                        break;

                    default:
                        str2 = "Null";
                        break;
                }
                switch (intType)
                {
                    case 1:
                        return ("H\x00f4m nay: " + str2 + " ng\x00e0y " + str3 + " th\x00e1ng " + str4 + " năm " + str5);

                    case 2:
                        return (str2 + " ng\x00e0y " + str3 + " th\x00e1ng " + str4 + " năm " + str5);

                    case 3:
                        return (str2 + ", " + str3 + "/" + str4 + "/" + str5);

                    case 4:
                        return (str5 + "/" + str4 + "/" + str3);

                    case 5:
                        return (str3 + "/" + str4 + "/" + str5);

                    case 6:
                        return (str3 + str4 + str5);

                    case 7:
                        return string.Concat(new object[] { hour, ":", minute, " - ", str3, "/", str4, "/", str5 });
                }
                return str;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str;
        }

        public static string EncodePasswordHashed(string pass, string salt)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(pass);
            byte[] src = Convert.FromBase64String(salt);
            byte[] dst = new byte[src.Length + bytes.Length];
            Buffer.BlockCopy(src, 0, dst, 0, src.Length);
            Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
            return Convert.ToBase64String(HashAlgorithm.Create(Membership.HashAlgorithmType).ComputeHash(dst));
        }

        public static string EncodingHTML(string ConvertStr)
        {
            return EncodingHTML(ConvertStr, new string[] { "&aacute;", "&agrave;", "&atilde;", "&acirc;", "&oacute;", "&ograve;", "&otilde;", "&ocirc;", "&uacute;", "&ugrave;", "&eacute;", "&egrave;", "&ecirc;", "&iacute;", "&igrave;", "&yacute;" }, new string[] { "\x00e1", "\x00e0", "\x00e3", "\x00e2", "\x00f3", "\x00f2", "\x00f5", "\x00f4", "\x00fa", "\x00f9", "\x00e9", "\x00e8", "\x00ea", "\x00ed", "\x00ec", "\x00fd" });
        }

        public static string EncodingHTML(string ConvertStr, string[] StrOld, string[] StrNew)
        {
            string str = ConvertStr;
            for (int i = 0; i < StrOld.Length; i++)
            {
                str = str.Replace(StrOld[i], StrNew[i]);
            }
            return str;
        }

        public static string EncryptStringMD5(string text)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "MD5").ToLower();
        }

        public static string EncryptStringSHA1(string text)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "SHA1").ToLower();
        }

        public static string fncCatChuoi(string s, int length)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "";
            }
            string[] strArray = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (strArray[0].Length > length)
            {
                return s;
            }
            StringBuilder builder = new StringBuilder();
            foreach (string str in strArray)
            {
                if ((builder + str).Length > length)
                {
                    return string.Format("{0}...", builder.ToString().TrimEnd(new char[] { ' ' }));
                }
                builder.Append(str + " ");
            }
            return string.Format("{0}...", builder.ToString().TrimEnd(new char[] { ' ' }));
        }

        public static int fncCnvNullToInt(object vobjValue)
        {
            int num = 0;
            try
            {
                if (fncIsBlankString(vobjValue))
                {
                    return 0;
                }
                if (!IsNumeric(vobjValue))
                {
                    return 0;
                }
                num = Convert.ToInt32(vobjValue);
            }
            catch (Exception exception)
            {
                oFileHelper.WriteLogErr("clsCommon", "fncCnvNullToInt", exception.ToString());
            }
            return num;
        }

        public static string fncCnvNullToString(object vobjValue)
        {
            string str = null;
            try
            {
                str = "";
                if (vobjValue != null)
                {
                    str = Convert.ToString(vobjValue);
                }
                return str;
            }
            catch (Exception exception)
            {
                oFileHelper.WriteLogErr("clsCommon", "fncCnvNullToString", exception.ToString());
            }
            return str;
        }

        public static string fncConvetDate2Thu(DateTime stTime)
        {
            int dayOfWeek = (int) stTime.DayOfWeek;
            if (dayOfWeek == 0)
            {
                return "Chủ Nhật";
            }
            return ("Thứ " + (dayOfWeek + 1));
        }

        public static string fncGetAppSettings(string vstrKeyName)
        {
            string str = "";
            try
            {
                str = WebConfigurationManager.AppSettings[vstrKeyName];
                if ((str == null) || (str.Length <= 0))
                {
                    throw new ApplicationException(vstrKeyName + "Kh\x00f4ng tồn tại trong 『web.config』");
                }
                return str;
            }
            catch (Exception exception)
            {
                HttpContext.Current.Response.Write(exception);
            }
            return str;
        }

        public static bool fncIsBlankString(object vstrValue)
        {
            try
            {
                return string.IsNullOrEmpty(fncCnvNullToString(vstrValue).Trim());
            }
            catch (Exception exception)
            {
                oFileHelper.WriteLogErr("clsCommon", "fncIsBlankString", exception.ToString());
            }
            return false;
        }

        public static bool IsNumeric(object vobjValue)
        {
            try
            {
                Convert.ToDouble(vobjValue);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string NgauNhien_SoChu(int codeCount)
        {
            string[] strArray = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".Split(new char[] { ',' });
            string str2 = "";
            int num = -1;
            Random random = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (num != -1)
                {
                    random = new Random((i * num) * ((int) DateTime.Now.Ticks));
                }
                int index = random.Next(0x24);
                if ((num != -1) && (num == index))
                {
                    return NgauNhien_SoChu(codeCount);
                }
                num = index;
                str2 = str2 + strArray[index];
            }
            return str2;
        }

        public static string RemoveAllTag(string strCont)
        {
            return Regex.Replace(strCont, "<[^>]+>", string.Empty);
        }

        public static string RemoveCRLF(string strCont)
        {
            return Regex.Replace(strCont, @"\s{2}", " ");
        }

        public static string RemoveSign4VietnameseString(string str)
        {
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {
                for (int j = 0; j < VietnameseSigns[i].Length; j++)
                {
                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
                }
            }
            return str;
        }

        public static string rpHash(string value)
        {
            int num = 0x1505;
            value = value.ToUpper();
            for (int i = 0; i < value.Length; i++)
            {
                num = ((num << 5) + num) + value[i];
            }
            return num.ToString();
        }

        public static string StringCut(string strT)
        {
            return strT.Substring(0, strT.Length - 1);
        }

        public static string StringLeft(string strT, int Left)
        {
            return strT.Substring(Left, strT.Length);
        }

        public static string StringRight(string strT, int Right)
        {
            return strT.Substring(0, strT.Length - Right);
        }

        public static string TachChuoi(string strValue)
        {
            return strValue.Replace("-", "").Replace("  ", " ").Replace(" ", ", ").Replace(",,", ",");
        }
    }
}

